#include<iostream>
#include<vector>
#include<cctype>
#include<fstream>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
using namespace std;
string toLowercase(const std::string &str) {
    string result=str;
    transform(result.begin(),result.end(),result.begin(),
        [](unsigned char c){return tolower(c);});
    return result;
}
struct qa{
	string q,a,a2;
};
vector<qa> qs={};
void sort(){
	sort(qs.begin(),qs.end(),[](qa a,qa b){return a.q.size()>b.q.size();});
}
void init(){
	srand(time(0));
	ifstream f("langlib.txt");
	string in,in2,in3;
	qa qao;
	while(getline(f,in)){
		ifstream f2(in.data());
		while(getline(f2,in),getline(f2,in2),getline(f2,in3)){
			in=toLowercase(in);
			qao.q=in,qao.a=in2,qao.a2=in3;
			qs.push_back(qao);
		}
		f2.close();
	}
	f.close();
	sort();
}
void chat(){
	string in;
	cout<<"Bot:Hello!I'm a simple chat bot.Type 'exit' or 'quit' to end the chat.\n";
	while(cout<<"You:",getline(cin,in),in=toLowercase(in),in!="exit"&&in!="quit"){
		if(in=="c-clean"){
			#ifdef __WIN32
			system("cls");
			#else
			system("clear");
			#endif
			cout<<"Bot:Hello!I'm a simple chat bot.Type 'exit' or 'quit' to end the chat.\n";
			continue;
		}else if(in=="c-import"){
			getline(cin,in);string in2;
			qa qao;
			ifstream f2(in.data());
			while(getline(f2,in),getline(f2,in2)){
				in=toLowercase(in);
				qao.q=in,qao.a=in2;
				qs.push_back(qao);
			}
			f2.close();
			continue;
		}
		cout<<"Bot:";
		bool nfd=1;
		for(const auto &q:qs){
			if(in.find(q.q)!=string::npos){
				nfd=0;
				if(rand()%2)cout<<q.a<<'\n';
				else cout<<q.a2<<'\n';
				break;
			}
		}
		if(nfd){
			cout<<"I'm not sure how to respond to that. Can you try asking differently?\n";
		}
	}
	cout<<"Bot:Goodbye!\n(input enter to continue)";
	getchar();
}
int main(){
	init();
	chat();
}
